package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.ZhuanjiaCustom;
import com.ruoyi.system.service.IDeptidOlddeptidService;
import com.ruoyi.web.controller.tool.JsonResult;
import com.ruoyi.web.controller.tool.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Zhuanjia;
import com.ruoyi.system.service.IZhuanjiaService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import springfox.documentation.spring.web.json.Json;

/**
 * 专家信息Controller
 * 
 * @author ruoyi
 * @date 2022-12-06
 */
@Controller
@RequestMapping("/system/zhuanjia")
public class ZhuanjiaController extends BaseController
{
    private String prefix = "system/zhuanjia";

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IZhuanjiaService zhuanjiaService;

    @Autowired
    private IDeptidOlddeptidService deptidOlddeptidService;

    @RequiresPermissions("system:zhuanjia:view")
    @GetMapping()
    public String zhuanjia()
    {
        return prefix + "/zhuanjia";
    }

    @RequiresPermissions("system:zhuanjia:customview")
    @GetMapping("/customzhuanjia")
    public String customzhuanjia()
    {
        return prefix + "/customzhuanjia";
    }

    //@RequiresPermissions("system:zhuanjiaguanliChaxuntongji:view")
    @GetMapping("/chaxuntongji")
    public String chaxuntongji()
    {
        return  "system/zhuanjiaguanli/chaxuntongji/index";
    }

    /**
     * 查询专家信息列表
     */
    @RequiresPermissions("system:zhuanjia:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Zhuanjia zhuanjia)
    {
        startPage();
        List<Zhuanjia> list = zhuanjiaService.selectZhuanjiaList(zhuanjia);
        return getDataTable(list);
    }

    /**
     * 导出专家信息列表
     */
    @RequiresPermissions("system:zhuanjia:export")
    @Log(title = "专家信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Zhuanjia zhuanjia)
    {
        List<Zhuanjia> list = zhuanjiaService.selectZhuanjiaList(zhuanjia);
        ExcelUtil<Zhuanjia> util = new ExcelUtil<Zhuanjia>(Zhuanjia.class);
        return util.exportExcel(list, "专家信息数据");
    }

    /**
     * 新增专家信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存专家信息
     */
    @RequiresPermissions("system:zhuanjia:add")
    @Log(title = "专家信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Zhuanjia zhuanjia)
    {
        return toAjax(zhuanjiaService.insertZhuanjia(zhuanjia));
    }

    /**
     * 修改专家信息
     */
    @RequiresPermissions("system:zhuanjia:edit")
    @GetMapping("/edit/{zhuanjiaid}")
    public String edit(@PathVariable("zhuanjiaid") String zhuanjiaid, ModelMap mmap)
    {
        Zhuanjia zhuanjia = zhuanjiaService.selectZhuanjiaByZhuanjiaid(zhuanjiaid);
        mmap.put("zhuanjia", zhuanjia);
        return prefix + "/edit";
    }

    /**
     * 修改保存专家信息
     */
    @RequiresPermissions("system:zhuanjia:edit")
    @Log(title = "专家信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Zhuanjia zhuanjia)
    {
        System.out.println(zhuanjia.getZhuanjiaid());
        return toAjax(zhuanjiaService.updateZhuanjia(zhuanjia));
    }

    /**
     * 删除专家信息
     */
    @RequiresPermissions("system:zhuanjia:remove")
    @Log(title = "专家信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zhuanjiaService.deleteZhuanjiaByZhuanjiaids(ids));
    }

    /**
     * 查询专家信息列表custom
     */
    //@RequiresPermissions("system:zhuanjia:customlist")
    @PostMapping("/customlist")
    @ResponseBody
    public JsonResult<?> customlist(ZhuanjiaCustom zhuanjia)
    {
        try {
            //将新单位ID转换为旧单位ID
            String mmDeptId = zhuanjia.getDeptid();
            zhuanjia.setDeptid(deptidOlddeptidService.DeptIdToOldDeptId(mmDeptId));

            List<Map<String,Object>> list = zhuanjiaService.selectZhuanjiaListCustom(zhuanjia);
            int mmCount = zhuanjiaService.countZhuanjiaListCustom(zhuanjia);


            PageInfo mmPageInfo = new PageInfo(zhuanjia.getPageindex(), zhuanjia.getPagesize(), mmCount);

            Map<String,Object> mmMap = new HashMap<>();
            mmMap.put("zhuanjialist",list);
            mmMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmMap);
        } catch (Exception e) {
            logger.error("zhuanjia -> customlist: "+e.getMessage());
            return JsonResult.getErrorResult("查询专家列表失败");
        }
    }

    /**
     * 专家管理-查询统计-列表
     */
    //@RequiresPermissions("system:zhuanjiaGuanliChaxuntongji:list")
    @PostMapping("/chaxuntongjilist")
    @ResponseBody
    public JsonResult<?> chaxuntongjilist(String deptid,
                                          String zhuanjia,
                                          String danwei,
                                          String danweileixing,
                                          String cengji,
                                          String gongzuodanwei,
                                          String guoji,
                                          String zhengjianleixing,
                                          String zhengjianhaoma,
                                          String ruxuannianfen,
                                          String zhuanjiazhuangtai,
                                          String zhuanjiazhuangtaibiandongshijian,
                                          int pageindex, int pagesize)
    {
        try {
            //获取当前用户
            Subject subject = ShiroUtils.getSubject();
            int mmCount = 0;
            //将新单位ID转换为旧单位ID
            String olddeptid = deptidOlddeptidService.DeptIdToOldDeptId(deptid);

            List<Map<String, Object>> list = new ArrayList<>();
            //专家中心超管、专家中心普通管理员、单位超管查本单位和所有下属单位
            if (subject.hasRole("zhongxin_commonadmin")||subject.hasRole("zhongxin_superadmin")||subject.hasRole("dept_superadmin"))
            {
                list = zhuanjiaService.chaxuntongjilist(olddeptid, zhuanjia, danwei, danweileixing, cengji,gongzuodanwei, guoji,
                        zhengjianleixing, zhengjianhaoma, ruxuannianfen, zhuanjiazhuangtai, zhuanjiazhuangtaibiandongshijian, pageindex, pagesize);
                mmCount = zhuanjiaService.chaxuntongjilistCount(olddeptid, zhuanjia, danwei, danweileixing, cengji,gongzuodanwei, guoji,
                        zhengjianleixing, zhengjianhaoma, ruxuannianfen, zhuanjiazhuangtai, zhuanjiazhuangtaibiandongshijian);
            }
            //单位普通管理员查本单位
            if (subject.hasRole("dept_commonadmin"))
            {
                 list = zhuanjiaService.chaxuntongjilistCommon(olddeptid, zhuanjia, danwei, danweileixing, cengji,gongzuodanwei, guoji,
                        zhengjianleixing, zhengjianhaoma, ruxuannianfen, zhuanjiazhuangtai, zhuanjiazhuangtaibiandongshijian, pageindex, pagesize);
                mmCount = zhuanjiaService.chaxuntongjilistCommonCount(olddeptid, zhuanjia, danwei, danweileixing, cengji,gongzuodanwei, guoji,
                        zhengjianleixing, zhengjianhaoma, ruxuannianfen, zhuanjiazhuangtai, zhuanjiazhuangtaibiandongshijian);
            }


            Map<String, Integer> count =new HashMap<>();

            //专家中心超管、单位超管查询 正常状态专家###名，其中1990年###名、1991年###名、1992年###名、1993年###名、1994年###名
            if (subject.hasRole("zhongxin_superadmin")||subject.hasRole("dept_superadmin"))
            {
                count = zhuanjiaService.Countchaxuntongji(olddeptid);
            }
            //专家中心普通管理员、单位普通管理员查询 正常状态专家###名，其中1990年###名、1991年###名、1992年###名、1993年###名、1994年###名
            if (subject.hasRole("dept_commonadmin")||subject.hasRole("zhongxin_commonadmin"))
            {
                count = zhuanjiaService.Countchaxuntongjicommon(olddeptid);
            }


            PageInfo mmPageInfo = new PageInfo(pageindex, pagesize, mmCount);

            Map<String,Object> mmMap = new HashMap<>();
            mmMap.put("zhuanjialist",list);
            mmMap.put("PageInfo", mmPageInfo);
            mmMap.put("CountInfo", count);
            return JsonResult.getSuccessResult(mmMap);
        } catch (Exception e) {
            logger.error("zhuanjia -> chaxuntongjilist: "+e.getMessage());
            return JsonResult.getErrorResult("查询专家管理查询统计列表失败");
        }
    }

    /**
     * 查询专家信息custom
     */
    //@RequiresPermissions("system:zhuanjia:findone")
    @PostMapping("/findone")
    @ResponseBody
    public JsonResult<?> findone(@RequestParam("zhuanjiaid") String ppZhuanjiaId)
    {
        try {
            Zhuanjia mmZhuanjia = zhuanjiaService.selectZhuanjiaByZhuanjiaid(ppZhuanjiaId);
            return mmZhuanjia==null?JsonResult.getErrorResult("查询专家失败"):JsonResult.getSuccessResult(mmZhuanjia);
        } catch (Exception e) {
            logger.error("zhuanjia -> findone: "+e.getMessage());
            return JsonResult.getErrorResult("查询专家失败");
        }
    }

    /**
     * 核增专家
     */
    //@RequiresPermissions("system:zhuanjia:hezeng")
    @PostMapping("/hezeng")
    @ResponseBody
    public JsonResult<?> hezeng(Zhuanjia zhuanjia,@RequestParam("deptid")String ppDeptId)
    {
        try {
            return zhuanjiaService.hezeng(zhuanjia, ppDeptId)==true?JsonResult.getSuccessResult("核增成功"):JsonResult.getErrorResult("核增失败");
        } catch (Exception e) {
            logger.error("zhuanjia -> hezeng: "+e.getMessage());
            return JsonResult.getErrorResult("核增专家失败");
        }
    }


}
